<html>
<head>
  <script src="../OLLoader.js"></script>
  <script type="text/javascript">
    var map; 
    function test_Control_KeyboardDefaults_constructor (t) {
        t.plan( 2 );
    
        control = new OpenLayers.Control.KeyboardDefaults();
        t.ok( control instanceof OpenLayers.Control.KeyboardDefaults, 
                "new OpenLayers.Control.KeyboardDefaults returns object" );
        t.eq( control.displayClass,  "olControlKeyboardDefaults", "displayClass is correct" );
    }

    function test_Control_KeyboardDefaults_destroy (t) {
        t.plan(2);
    
        map = new OpenLayers.Map('map');
        var control = new OpenLayers.Control.KeyboardDefaults();
        map.addControl(control);
        t.ok(control.handler != null, "control.handler is created");
        control.destroy();
        t.ok(control.handler == null, "control.handler is null after destroy");
        map.destroy();
    }

    function test_Control_KeyboardDefaults_addControl (t) {
        t.plan( 4 );

        map = new OpenLayers.Map('map');
        control = new OpenLayers.Control.KeyboardDefaults();
        t.ok( control instanceof OpenLayers.Control.KeyboardDefaults, 
                "new OpenLayers.Control.KeyboardDefaults returns object" );
        t.ok( map instanceof OpenLayers.Map, 
                "new OpenLayers.Map creates map" );
        map.addControl(control);
        t.ok( control.map === map, "Control.map is set to the map object" );
        t.ok( OpenLayers.Util.indexOf(map.controls, control), "map.controls contains control" );
    }

    /* When interpretting
     * the keycodes below (including the comments associated with them),
     * consult the URL below. For instance, the Safari browser returns
     * "IE keycodes", and so is supported by any keycode labeled "IE".
     * 
     * Very informative URL:
     *    http://unixpapa.com/js/key.html
     */
    function test_Control_KeyboardDefaults_KeyDownEvent (t) {
        t.plan( 25 );

        var evt = {which: 1}, pans = [], zoomIns = 0, zoomOuts = 0;

        map = new OpenLayers.Map('map');

        // mock "pan", "zoomIn" and "zoomOut"
        map.pan = function(dx, dy) {
            pans.push({dx: dx, dy: dy});
        };
        map.zoomIn = function() {
            zoomIns++;
        };
        map.zoomOut = function() {
            zoomOuts++;
        };

        var layer = new OpenLayers.Layer.WMS("Test Layer", 
            "http://octo.metacarta.com/cgi-bin/mapserv?",
            {map: "/mapdata/vmap_wms.map", layers: "basic"});
        map.addLayer(layer);

        var control = new OpenLayers.Control.KeyboardDefaults({
            slideFactor: 100
        });
        map.addControl(control);
        
        map.setCenter(new OpenLayers.LonLat(0, 0), 4);

        // Start new test.
        evt.keyCode = OpenLayers.Event.KEY_LEFT;
        control.defaultKeyPress(evt);
        t.eq(pans.length, 1, '[KEY_LEFT] pan called once');
        t.eq(pans[0], {dx: -100, dy: 0},
             '[KEY LEFT] pan called with expected args');

        evt.keyCode = OpenLayers.Event.KEY_RIGHT;
        control.defaultKeyPress(evt);
        t.eq(pans.length, 2, '[KEY_RIGHT] pan called once');
        t.eq(pans[1], {dx: 100, dy: 0},
             '[KEY RIGHT] pan called with expected args');

        evt.keyCode = OpenLayers.Event.KEY_UP;
        control.defaultKeyPress(evt);
        t.eq(pans.length, 3, '[KEY_UP] pan called once');
        t.eq(pans[2], {dx: 0, dy: -100},
             '[KEY UP] pan called with expected args');

        evt.keyCode = OpenLayers.Event.KEY_DOWN;
        control.defaultKeyPress(evt);
        t.eq(pans.length, 4, '[KEY_DOWN] pan called once');
        t.eq(pans[3], {dx: 0, dy: 100},
             '[KEY DOWN] pan called with expected args');

        evt.keyCode = 33;
        control.defaultKeyPress(evt);
        t.eq(pans.length, 5, '[33] pan called once');
        t.eq(pans[4], {dx: 0, dy: -384},
             '[33] pan called with expected args');

        evt.keyCode = 34;
        control.defaultKeyPress(evt);
        t.eq(pans.length, 6, '[34] pan called once');
        t.eq(pans[5], {dx: 0, dy: 384},
             '[34] pan called with expected args');

        evt.keyCode = 35;
        control.defaultKeyPress(evt);
        t.eq(pans.length, 7, '[35] pan called once');
        t.eq(pans[6], {dx: 768, dy: 0},
             '[35] pan called with expected args');

        evt.keyCode = 36;
        control.defaultKeyPress(evt);
        t.eq(pans.length, 8, '[36] pan called once');
        t.eq(pans[7], {dx: -768, dy: 0},
             '[36] pan called with expected args');

        evt.keyCode = 43;
        control.defaultKeyPress(evt);
        t.eq(zoomIns, 1, '[43] zoomIn called once');

        evt.keyCode = 61;
        control.defaultKeyPress(evt);
        t.eq(zoomIns, 2, '[61] zoomIn called once');

        evt.keyCode = 187;
        control.defaultKeyPress(evt);
        t.eq(zoomIns, 3, '[187] zoomIn called once');

        evt.keyCode = 107;
        control.defaultKeyPress(evt);
        t.eq(zoomIns, 4, '[107] zoomIn called once');

        evt.keyCode = 107;
        control.defaultKeyPress(evt);
        t.eq(zoomIns, 5, '[107] zoomIn called once');

        evt.keyCode = 45;
        control.defaultKeyPress(evt);
        t.eq(zoomOuts, 1, '[45] zoomOut called once');

        evt.keyCode = 109;
        control.defaultKeyPress(evt);
        t.eq(zoomOuts, 2, '[109] zoomOut called once');

        evt.keyCode = 189;
        control.defaultKeyPress(evt);
        t.eq(zoomOuts, 3, '[189] zoomOut called once');

        evt.keyCode = 95;
        control.defaultKeyPress(evt);
        t.eq(zoomOuts, 4, '[95] zoomOut called once');

        map.destroy();
    }


  </script>
</head>
<body>
    <div id="map" style="width: 1024px; height: 512px;"/>
</body>
</html>
